package cn.edu.zut.pms.faker;

import com.github.javafaker.Faker;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Random;

public class FakerLogisticsData {
    public static void main(String[] args) {
        // 数据库连接信息
        String url = "jdbc:mysql://192.168.17.150:3306/soil?allowPublicKeyRetrieval=true&useSSL=false";
        String user = "root";
        String password = "000000";

        // 创建中文Faker实例
        Faker faker = new Faker(new Locale("zh_CN"));
        Random random = new Random();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 定义可能的枚举值
        String[] deliveryStatus = {"已送达", "配送中", "已取消", "待发货", "配送延迟"};
        String[] products = {"电子产品", "服装鞋帽", "食品饮料", "家居用品", "图书文具", "美妆个护"};
        String[] feedbacks = {"满意", "一般", "不满意", "未评价", "质量很好", "物流太慢", "包装破损"};
        String[] regions = {"华北区", "华东区", "华南区", "西北区", "西南区", "东北区", "华中区"};

        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            // 插入数据的SQL语句
            String insertSql = "INSERT INTO dwd_logistics_data (" +
                    "order_id, sales_time, delivery_time, delivery_status, " +
                    "product, product_feedback, sales_region, quantity, sales_amount" +
                    ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

            try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql)) {
                for (int i = 0; i < 200; i++) { // 生成200条测试数据
                    // 生成订单号
                    String orderId = "ORD" + faker.number().numberBetween(1000000, 9999999);

                    // 生成销售时间（近一年）
                    LocalDateTime salesTime = LocalDateTime.now()
                            .minusDays(faker.number().numberBetween(0, 365))
                            .minusHours(faker.number().numberBetween(0, 24));

                    // 生成交货时间（销售时间之后或为null）
                    LocalDateTime deliveryTime = null;
                    String status = deliveryStatus[random.nextInt(deliveryStatus.length)];
                    if (!status.equals("已取消") && !status.equals("待发货")) {
                        deliveryTime = salesTime.plusHours(faker.number().numberBetween(2, 72));
                    }

                    // 随机选择枚举值
                    String product = products[random.nextInt(products.length)];
                    String feedback = feedbacks[random.nextInt(feedbacks.length)];
                    String region = regions[random.nextInt(regions.length)];

                    // 生成数量和金额
                    int quantity = faker.number().numberBetween(1, 50);
                    double amount = faker.number().randomDouble(2, 10, 5000);

                    // 设置参数
                    preparedStatement.setString(1, orderId);
                    preparedStatement.setString(2, salesTime.format(formatter));
                    preparedStatement.setString(3, deliveryTime != null ? deliveryTime.format(formatter) : null);
                    preparedStatement.setString(4, status);
                    preparedStatement.setString(5, product);
                    preparedStatement.setString(6, feedback);
                    preparedStatement.setString(7, region);
                    preparedStatement.setInt(8, quantity);
                    preparedStatement.setDouble(9, amount);

                    preparedStatement.executeUpdate();
                }
                System.out.println("数据插入完成，共插入 200 条记录");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}